Register-Lib PowerShell.Objects

filter Expand-Property($prop) {$target=$_
    $prop | % { $target | select -ExpandProperty $_ }
}

function New-PSObject ($PropertyHash) {
    New-Object -TypeName PSObject -Property:$PropertyHash
}

filter Add-NoteProperty($Name, $Value, [ScriptBlock] $Expression, [switch]$Force=$True){
    $currentValue = $value
    if(($currentValue -eq $null) -and ($Expression)){
        $currentValue = (& $Expression $_)
    }    
    $_ | Add-Member -MemberType NoteProperty -Name:$name -Value:$currentValue -PassThru -Force:$Force
}


filter Append-Property{ 
    param(
        $PropertyHash, 
        [switch] $Overwrite, 
        [switch]$CreateObject=$True,
        [switch] $PassThru=$True
    )
    $target = $_
    if(!$target){
        if($CreateObject){
            return New-PSObject -PropertyHash:$PropertyHash
        }
    } 
    else {
        if($PropertyHash) {
            foreach($key in $PropertyHash.Keys){
                $value = $PropertyHash[$key]
                
                if($value.Length -gt 1) {
                    $firstValue = $value[0]
                    $secondValue = $value[1]
                } else {
                    $firstValue = $value
                }
                
                if($firstValue -is [ScriptBlock]){
                    $memberType = 'ScriptProperty'
                    $target | Add-Member -MemberType:$memberType -Name:$key -Value:$firstValue -SecondValue:$secondValue -Force:$Overwrite 
                }  else {
                    $memberType = 'NoteProperty'
                    $target | Add-Member -MemberType:$memberType -Name:$key -Value:$firstValue -Force:$Overwrite 
                }
                
            }
        }
        $target
    }

}
set-alias extend Append-Property



# from 
# http://powershell.com/cs/blogs/tips/archive/2008/11/14/converting-hash-tables-to-objects.aspx

function ConvertTo-Object {

	begin { $object = New-Object Object }

	process {
		$_.GetEnumerator() | ForEach-Object { 
			Add-Member -inputObject $object -memberType NoteProperty -name $_.Name -value $_.Value 
		}  
	}

	end { $object }

}

filter ConvertTo-Objects {
	$_ | % { $_ | ConvertTo-Object }
}

$GetType = @{Name='CLRType';Expression={ $_.GetType() }} 
filter GetType { $_.GetType() }
filter AddType { 
    $_ | Add-Member -MemberType NoteProperty -Name Type -Value $_.GetType()     
    $_
}